<?php
/**
 * 第三方合作站点数据统计类
 * Created by
 * Auth: farmer [bejea#qq.com]
 * Careate time: 2017/7/28.'-'.16:42
 */
 namespace Home\Controller;
 use Think\Controller;
 class TpsController extends Controller {

	 /**
	  * @desc: 第三方合作方其下站点数据接口
	  * @Author: liwenlong
	  * @update: 2017-07-18
	  **/
	 public function my_station_data($dates)
	 {
		 //前台需要传个日期  格式20170718
		 $station_id = M('tp_third_partner')->field('f_station_id')->select();   //获取与雅居乐合作的站点ID

         $dbtablename = 'tp_parcel_info_' . substr($dates, 0, 6);  //获取表名
		 $today = $dates . "000000";           //开始时间
		 $Yesterday = $dates . "235959";           //结束时间

		 $today = strtotime($today);

		 $where['f_arrive_timestamp'] = array('between', array($today,strtotime($Yesterday)));       //between表达式


         for ($i = 0; $i < count($station_id); $i++) {

             $data[$i] = M($dbtablename)->alias('p')
                 ->field('
            u.f_station_name,
            p.f_company_name,
            p.f_parcel_status,
            p.f_recipient_name,
            p.f_arrive_time,
            p.f_out_time,
            p.f_recipient_mobile
            ')
                 ->join('left join tp_user as u on p.f_station_id=u.f_user_id')
                 ->where(['f_station_id' => $station_id[$i]['f_station_id']])
                 ->where($where)
                 ->select();
         }
         foreach($data as $value){
             foreach($value as $v){
                 $list[]=$v;
             }
         }
//		 隐藏姓名第二位
         $datalen=count($list);
		 for ($i=0;$i<$datalen;$i++){
			 $strlen     = mb_strlen($list[$i]['f_recipient_name'], 'utf-8');
			 $firstStr     = mb_substr($list[$i]['f_recipient_name'], 0, 1, 'utf-8');
			 $lastStr     = mb_substr($list[$i]['f_recipient_name'], -1, 1, 'utf-8');
			 $list[$i]['f_recipient_name']=$strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($list[$i]['f_recipient_name'], 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
		 }

		 for ($i=0;$i<$datalen;$i++){
             $list[$i]['f_recipient_mobile'] = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $list[$i]['f_recipient_mobile']);
         }


		 if (empty($data)) {              //检测是否有返回值
			 $this->ajaxReturn('无返回值');
		 } else {
			 return $list;
		 }

	 }

	 /**
	  * @desc	第三方合作伙伴所属站点包裹数据查询
	  * @param:
	  * 	date	string	指定查询数据日期 格式 20170630
	  * 	export	int		是否导出 1是，不导出返回json数据
	  * 	token	string	用户Token
	  * @return:
	  * 	msg		string	提示
	  * 	code	int		状态码 0 正常, 其它看msg
	  * 	data	array	返回的数据
	  *
	  * @Author: liwenlong
	  * @update: 2017-07-18
	  **/
	 public function  idata(){
//         dump(I('date'));exit();
		//雅居乐临时token	：	8c928fb2ce098fd77bbfb70c0bde53a4
		 if(!I('date')){
			 echo json_encode(array('code' =>1, 'msg'=> '请指定查询日期'));
			 exit;
		 }else{
			 if (I('token') !== md5('yjl2018')){
				 echo json_encode(array('code' =>2, 'msg'=> 'Token错误'));
				 exit;
			 }
			 $dates= I('date');
		 }
		 $innerdata=$this->my_station_data($dates);


		 if(I('export') != 1){
			 $total = count($innerdata);
			 echo json_encode(array('code' =>0,'msg'=> '','total' => $total, 'data' => $innerdata));
			 exit;
		 }

//		 dump($innerdata);exit();
		 $table = '';

		 $table .= "<table>
            <thead>
                <tr>
                    <th class='name'>站点名称</th>
                    <th class='name'>快递公司</th>
                    <th class='name'>收件人手机号</th>
                    <th class='name'>包裹状态</th>
                    <th class='name'>收件人姓名</th>
                    <th class='name'>入库时间</th>
                    <th class='name'>出库时间</th>
                    <th class='name'>备注</th>
                </tr>
            </thead>
            <tbody>";

         foreach ($innerdata as $v) {
             $table .= "<tr>
                    <td class='name'>{$v['f_station_name']}</td>
                    <td class='name'>{$v['f_company_name']}</td>
                     <td class='name'>{$v['f_recipient_mobile']}</td>
                    <td class='name'>";
                 if($v['f_parcel_status']==0){
                     $table .= "已到站";
                 }else if($v['f_parcel_status']==1){
                     $table .= "已提货";
                 }else if($v['f_parcel_status']==2){
                     $table .= "通知中";
                 }else if($v['f_parcel_status']==3){
                     $table .= "异常件";
                 }else if($v['f_parcel_status']==4){
                     $table .= "一次拒收";
                 }else if($v['f_parcel_status']==5){
                     $table .= "通知失败";
                 }else if($v['f_parcel_status']==6){
                     $table .= "二次拒收";
                 }
             $table .= "
                </td>
                    <td class='name'>{$v['f_recipient_name']}</td>
                    <td class='name'>{$v['f_arrive_time']}&nbsp;</td>
                    <td class='name'>{$v['f_out_time']}&nbsp;</td>
                </tr>";
         }

         $table .= "</tbody>
        </table>";

		 //通过header头控制输出excel表格
		 header("Pragma: public");
		 header("Expires: 0");
		 header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		 header("Content-Type:application/force-download");
		 header("Content-Type:application/vnd.ms-execl");
		 header("Content-Type:application/octet-stream");
		 header("Content-Type:application/download");;
		 header('Content-Disposition:attachment;filename="'.I('date').'日数据.xls"');
		 header("Content-Transfer-Encoding:binary");

		 echo $table;

		 exit();
	 }

 }